package com.wealth.vouchermarket.web.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.wealth.cart.entity.Cart;
import com.wealth.commodity.entity.Comment;
import com.wealth.commodity.entity.Goods;
import com.wealth.commodity.service.ICommentService;
import com.wealth.commodity.service.IGoodsService;
import com.wealth.commodity.vo.GoodsVO;
import com.wealth.order.service.IOrderService;
import com.wealth.system.web.controller.BaseController;

/**
 * 惠生活 - 商品
 * 
 * @author chan
 */
@Controller
@RequestMapping(value="/vouchermarket/goods")
public class VoucherMarketGoodsController extends BaseController{

	Logger logger = Logger.getLogger(VoucherMarketGoodsController.class);
	
	@Autowired
	private IGoodsService goodsService;
	@Autowired
	private ICommentService commentService;
	@Autowired
	private IOrderService orderService;
	/** 
	 * 详情页面
	 */
	@RequestMapping(value="/{id}")
	public String info(Model model,HttpSession session,@PathVariable Long id) {
		try{
			//TODO 临时使用
			int[] arryId = new int[]{1121,1122,1123,1124,1127,1128,539};
			for (int i = 0; i < arryId.length; i++) {
				if(id==arryId[i]){
					return "redirect:/commodity/goodsinfo?goodsId="+id+"&marketId="+3L;  
				}
			}
			
			//商品
			Goods goods =goodsService.get(id);
			model.addAttribute("goods", goods);
			
			Long merchantid = goods.getMerchant().getId();
			
			//商户最近的评论  前offset条 
			List<Comment> commentList = commentService.getLatestComment(merchantid, 1);
			model.addAttribute("commentList", commentList);
			
			//商户评价平均分数
			Integer avgScore = commentService.getAvgScore(id);
			model.addAttribute("avgScore",avgScore);

			//商户评价总数
			Integer commentNum = commentService.getCommentNum(id);
			model.addAttribute("commentNum",commentNum);
			
			//商户月消费人数
			Date start = DateTime.now().dayOfMonth().withMinimumValue().toDate();
			Date end = DateTime.now().dayOfMonth().withMaximumValue().toDate();
			Integer avgConsumeNumber = orderService.avgConsumeNumberByMerchantId(merchantid, start, end);
			model.addAttribute("avgConsumeNumber",avgConsumeNumber);
			
			//商户平均消费
			Integer avgTotal = orderService.avgTotalByMerchantId(merchantid);
			model.addAttribute("avgTotal",avgTotal);
			
			return "vouchermarket/goods/info";
		}catch(Exception e){
			return "system/500";
		}
	}
	
	
	
}
